﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.IO;
using System.Web.UI;
using ChoViet.Model;
using ChoViet.CMS.Properties;

namespace ChoViet.Lib.Helpers
{
	public static class AdHelpers
	{

        public static string ProductCategoryLink(this HtmlHelper htmlHelper, Demand demand, Category category, City city, AdAttribute adAttribute, AdAttributeValue adAttributeValue, int cusTypeId)
		{
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var totalAds = eDataContext.Ads.Where(e => e.CatID == category.ID || e.Category.ParentCategory.ID == category.ID).Count();
            var t = totalAds > 0? string.Format("{0:#,#}",totalAds) : "0";
            return string.Format("<a href=\"{0}\" />{1}..<span class=\"catecount\"/>{2}</span></a>",ProductCategoryRoute(htmlHelper, demand, category, city, adAttribute, adAttributeValue, cusTypeId), category != null && !string.IsNullOrEmpty(category.Name) ? category.Name : "Hàng hóa",t);
		}

		public static string ProductCategoryRoute(this HtmlHelper htmlHelper, Demand demand,Category category, City city,AdAttribute adAttribute,AdAttributeValue adAttributeValue, int cusTypeId)
		{
            var link = string.Format("/{0}/{1}/{2}/{3}/{4}/{5}", demand != null && !string.IsNullOrEmpty(demand.ShortName) ? demand.ShortName : "mua-ban",
                    category != null && !string.IsNullOrEmpty(category.ShortName) ? category.ShortName : "hang-hoa",
                    city != null && !string.IsNullOrEmpty(city.Name) ? city.ShortName : "ca-nuoc",
                    adAttribute != null && !string.IsNullOrEmpty(adAttribute.Name) ? TextHelper.ToUrlFriendly(htmlHelper, adAttribute.Name) : "thong-tin",
                    adAttributeValue != null && !string.IsNullOrEmpty(adAttributeValue.Value) ? TextHelper.ToUrlFriendly(htmlHelper, adAttributeValue.Value) : "lien-he",
                    cusTypeId != 0 ? cusTypeId : 0);
			return link;
		}
        public static string AdDetailRoute(this HtmlHelper htmlHelper, Ad Ad)
        {
            var link = string.Format("/{0}/{1}/{2}-{3}.html", TextHelper.ToUrlFriendly(htmlHelper, Ad.City.Name),
                                    TextHelper.ToUrlFriendly(htmlHelper, Ad.Category.Name), TextHelper.ToUrlFriendly(htmlHelper, Ad.Title), Ad.ID);
            return link;
        }
        public static string ShopDetailRoute(this HtmlHelper htmlHelper, Shop shop)
        {
            var link = string.Format("/{0}/{1}/{2}-{3}.html", TextHelper.ToUrlFriendly(htmlHelper, shop.City.Name),
                                    TextHelper.ToUrlFriendly(htmlHelper, shop.Category.Name), TextHelper.ToUrlFriendly(htmlHelper, shop.Name), shop.ID);
            return link;
        }
        public static string AdDetailRoute(Ad ad)
        {
            var link = string.Format("/{0}/{1}/{2}-{3}.html", TextHelper.ToUrlFriendly(ad.City.Name),
                                    TextHelper.ToUrlFriendly(ad.Category.Name), TextHelper.ToUrlFriendly(ad.Title), ad.ID);
            return link;
        }
		public static string ProductLink(this HtmlHelper htmlHelper, Ad Product)
		{
			return string.Format("<a href=\"{0}\" />{1}</a>", ProductRoute(htmlHelper, Product), Product.Title);
		}

        public static string ProductRoute(this HtmlHelper htmlHelper, Ad Product)
        {
            var link = string.Format("/{0}-{1}/{2}-p-{3}.html", Product.CatID, TextHelper.ToUrlFriendly(htmlHelper,Product.Category.Name), TextHelper.ToUrlFriendly(htmlHelper,Product.Title), Product.ID);
            return link;
        }

		public static string ProductCategoryMenu(this HtmlHelper htmlHelper,Demand demand, List<Category> rootCategories, Category activeCategory,City city,AdAttribute adAttribute, AdAttributeValue AdAttributeValue,int cusTypeId, string cssMenuClass, string cssActiveClass)
		{
			var b = new StringBuilder();

			foreach (var item in rootCategories)
			{
				b.Append(CategoryMenuItem(htmlHelper,demand, item, activeCategory,city,adAttribute,AdAttributeValue,cusTypeId, cssActiveClass));
			}
            return string.Format("<ul id='rootleft' class='list-menu-left' >{0}</ul>", b.ToString());
		}

        public static string CategoryMenuItem(this HtmlHelper htmlHelper, Demand demand, Category p, Category c, City city, AdAttribute adAttribute, AdAttributeValue adAttributeValue,int cusTypeId, string cssActiveClass)
		{
			var b = new StringBuilder(ProductCategoryLink(htmlHelper,demand,p,city,adAttribute,adAttributeValue,cusTypeId));

            //var recurseChildCategory = AdManager.GetChildCategory(p.ID); //Old
            var recurseChildCategory = AdManager.GetChildCategory(p);

            //12-04-2013 VietLV Repaire not check active category => show all childen category
            //if (p.Categories.Count > 0 && c != null && (c.ID == p.ID || recurseChildCategory.Where(item => item.ID == c.ID).Count() >= 1)) // Old
            if (p.Categories.Count > 0 && c != null)  // New
			{
				var b1 = new StringBuilder();
				foreach (var cat in p.Categories)
					b1.Append(CategoryMenuItem(htmlHelper,demand,cat, c,city,adAttribute,adAttributeValue,cusTypeId, cssActiveClass));
				b.AppendFormat("<ul style=\"padding-left: 25px;\">{0}</ul>", b1);
			}

			if (p != null && c != null && p.ID == c.ID)
			{
                return string.Format("<li class=\"{0}\"><div class=\"item item{1}\"></div>{2}</li>", cssActiveClass ?? "",p.ID, b);
			}
			else
			{
                return string.Format("<li><div class=\"item item{0}\"></div>{1}</li>", p.ID, b);
			}
		}
	}
}
